package com.lier.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lier.gulimall.product.entity.ProductAttrValueEntity;
import com.lier.gulimall.product.service.ProductAttrValueService;
import com.lier.gulimall.product.vo.AttrResponseVo;
import com.lier.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lier.gulimall.product.entity.AttrEntity;
import com.lier.gulimall.product.service.AttrService;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.R;



/**
 * 商品属性
 *
 * @author lier
 * @email 664478853@qq.com
 * @date 2025-06-06 14:14:16
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("{attrType}/list/{catelogId}")
    public R listAttr(@RequestParam Map<String, Object> params, @PathVariable Long catelogId, @PathVariable String attrType){
        PageUtils page = attrService.listAttr(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    @GetMapping("base/listforspu/{spuId}")
    public R listForSpu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> data = productAttrValueService.baseAttrListForSpu(spuId);

        return R.ok().put("data", data);
    }

    @PostMapping("update/{spuId}")
    public R updateSpuAttr(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponseVo attr = attrService.getAttrDetail(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
